import { getMessage, TextWidget } from '@inbiz/react';
import moment from 'moment';

export const formatSiteName = (siteType: number | string) => {
    switch ('' + siteType) {
        case '0':
            return <TextWidget>LangManage.webPageSide</TextWidget>;
        case '1':
            return <TextWidget>LangManage.mobileTerminal</TextWidget>;
        case '2':
            return <TextWidget>LangManage.mgtSide</TextWidget>;
        case '3':
            return <TextWidget>LangManage.processForm</TextWidget>;
        default:
            return <TextWidget>LangManage.webPageSide</TextWidget>;
    };
};

export const getLangSiteType = (siteType: number | string) => {
    switch (siteType) {
        case 0:
        case '0':
            return 1;
        case 1:
        case '1':
            return 2;
        case 2:
        case '2':
            return 4;
        default:
            return 0;
    };
};

export const formatSiteType = (siteType: number) => {
    switch (siteType) {
        case 1:
            return 'Web端';
        case 2:
            return '移动端';
        case 4:
            return '管理端';
        case 3:
            return 'Web端,移动端';
        case 5:
            return 'Web端,管理端';
        case 6:
            return '移动端,管理端';
        case 7:
            return 'Web端,移动端,管理端';
        default:
            return '';
    };
};

export const formatSiteTypeToArray = (siteType: number) => {
    switch (siteType) {
        case 1:
            return [1];
        case 2:
            return [2];
        case 4:
            return [4];
        case 3:
            return [1, 2];
        case 5:
            return [1, 4];
        case 6:
            return [2, 4];
        case 7:
            return [1, 2, 4];
        default:
            return [];
    };
};

export const formatQueryFormData = (formData: any) => {
    let _formData: any = {};
    for (const key in formData) {
        if (formData[key] || formData[key] == 0) {
            _formData[key] = formData[key];
        };
    };
    return _formData;
};

export const componentLangTypeEnum = (type: number) => {
    switch (type) {
        case 0:
            return '全部组件';
        case 1:
            return '系统组件';
        case 2:
            return '自定义组件';
        case 4:
            return '多语言提示';
        default:
            return '';
    };
};

export const getSiteType = (siteType: string) => {
    switch (siteType) {
        case '0':
            return 'web';
        case '1':
            return 'wap';
        case '2':
            return 'mgt';
        default:
            return '';
    };
};

export const getTreeCurrentData = (outId: string, treeData: any[]) => {
    let currentRow: any = null;
    for (let i = 0; i < treeData.length; i++) {
        if (!currentRow) {
            if (treeData[i]?.OutId == outId) {
                currentRow = treeData[i];
                break;
            } else {
                if (treeData[i]?.Children && treeData[i]?.Children?.length > 0) {
                    currentRow = getTreeCurrentData(outId, treeData[i]?.Children);
                } else {
                    continue;
                };
            };
        };
    };
    return currentRow;
};

export const getQueryParams = (isForm: boolean, currentType: string | number, isApp: boolean, selectedRow: any, siteInfo: any) => {
    let _queryParams: any = {};
    if (isForm) {
        _queryParams['isForm'] = true;
        _queryParams['queryLanguageType'] = Number(currentType);
        _queryParams['dataVersionId'] = selectedRow?.DataVersionId || '';
        if (currentType == '0') {
            if (!isApp) {
                _queryParams['formOutId'] = selectedRow?.OutId || '';
            };
        } else {
            if (!isApp) {
                _queryParams['pageOutId'] = selectedRow?.OutId || '';
            };
        };
    } else {
        _queryParams = isApp ? {
            siteOutId: siteInfo?.OutId || '',
            dataVersionId: selectedRow?.DataVersionId || '',
            queryLanguageType: Number(currentType),
            siteType: getLangSiteType(siteInfo?.SiteType),
        } : {
            siteOutId: siteInfo?.OutId || '',
            dataVersionId: selectedRow?.DataVersionId || '',
            queryLanguageType: Number(currentType),
            siteType: getLangSiteType(siteInfo?.SiteType),
            pageOutId: selectedRow?.OutId || '',
        };
    };
    return _queryParams;
};

export const compare = (property: string) => {
    return function (a, b) {
        const value1 = moment(a[property]).unix();
        const value2 = moment(b[property]).unix();
        return value2 - value1;
    };
};

export const langEnum = [
    { code: 'zh-cn', name: '简体中文' },
    { code: 'zh-hant', name: '繁体中文' },
    { code: 'en', name: 'English' },
    { code: 'ms', name: 'Melayu' },
    { code: 'th', name: 'ไทย' },
    { code: 'es', name: 'español' },
    { code: 'id', name: 'Indonesia' },
    { code: 'ar', name: 'العربية' },
    { code: 'bg', name: 'български' },
    { code: 'ca', name: 'català' },
    { code: 'cs', name: 'čeština' },
    { code: 'da', name: 'dansk' },
    { code: 'de', name: 'Deutsch' },
    { code: 'el', name: 'Ελληνικά' },
    { code: 'et', name: 'eesti' },
    { code: 'fa', name: 'føroyskt' },
    { code: 'fi', name: 'suomi' },
    { code: 'fr', name: 'français' },
    { code: 'ga', name: 'Gaeilge' },
    { code: 'he', name: 'עברית' },
    { code: 'hi', name: 'हिन्दी' },
    { code: 'hr', name: 'hrvatski' },
    { code: 'hu', name: 'magyar' },
    { code: 'hy', name: 'հայերեն' },
    { code: 'is', name: 'íslenska' },
    { code: 'it', name: 'italiano' },
    { code: 'ja', name: '日本語' },
    { code: 'kn', name: 'ಕನ್ನಡ' },
    { code: 'ko', name: '한국어' },
    { code: 'ku', name: 'Kurdî' },
    { code: 'lv', name: 'latviešu' },
    { code: 'mk', name: 'македонски' },
    { code: 'mn', name: 'монгол' },
    { code: 'nb', name: 'norsk' },
    { code: 'ne', name: 'नेपाली' },
    { code: 'nl', name: 'Nederlands' },
    { code: 'pl', name: 'polski' },
    { code: 'pt', name: 'português' },
    { code: 'ro', name: 'română' },
    { code: 'ru', name: 'русский' },
    { code: 'sk', name: 'slovenčina' },
    { code: 'sl', name: 'slovenščina' },
    { code: 'sr', name: 'српски' },
    { code: 'sv', name: 'svenska' },
    { code: 'ta', name: 'தமிழ்' },
    { code: 'tr', name: 'Türkçe' },
    { code: 'uk', name: 'українська' },
    { code: 'vi', name: 'Tiếng Việt' },
];